package com.sky.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.constant.TypeConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Setmeal;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @projectName: sky-take-out_1
 * @package: com.sky.service.impl
 * @className: CategoryServiceImpl
 * @author: Eric
 * @description: TODO
 * @date: 2023/10/13 17:12
 * @version: 1.0
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public PageResult page(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        List<Category> paginationList = categoryMapper.selectCategoryByNameAndType(categoryPageQueryDTO.getName(),categoryPageQueryDTO.getType());
        PageInfo<Category> pageInfo = PageInfo.of(paginationList);
        return new PageResult(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public void addCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        category.setStatus(StatusConstant.ENABLE);
        categoryMapper.addCategory(category);
    }

    @Override
    public void deleteCategoryById(Long id) {
        Category category = categoryMapper.selectCategoryById(id);
        if (Objects.equals(category.getName(), TypeConstant.DISH)){
            if (Objects.equals(category.getStatus(),StatusConstant.ENABLE)){    // 起售状态不能删除
                throw new RuntimeException(MessageConstant.DISH_ON_SALE);
            }
            Integer dishCount = dishMapper.selectCountById(category.getId());   // 关联了菜品的不能删除
            if (dishCount!=0){
                throw new RuntimeException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
            }
        }
        if (Objects.equals(category.getName(), TypeConstant.PACKAGE)){
            if (Objects.equals(category.getStatus(),StatusConstant.ENABLE)){
                throw new RuntimeException(MessageConstant.SETMEAL_ON_SALE);
            }
            Integer setmealCount = setmealMapper.selectCountById(category.getId());
            if (setmealCount!=0){
                throw new RuntimeException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
            }
        }
        categoryMapper.deleteCategoryById(category);
    }

    @Override
    public void modifyClassification(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        categoryMapper.modifyClassification(category);
    }

    @Override
    public void changeStatus(Long id, Integer status) {
        Category category = categoryMapper.getCategoryById(id);
        category.setStatus(status);
        categoryMapper.modifyClassification(category);
    }

    @Override
    public List<Category> queryByType(Integer type) {
        List<Category> categoryList = categoryMapper.queryByType(type);
        return categoryList;
    }
}
